class Game
{
    constructor(el){
        //保存地图对象
        this.el = document.querySelector(el)
        // 创建地图
        this.map = new Map(this.el)
        this.map.create()
        // 创建食物
        this.food = new Food(this.el)
        this.food.create()
        //创建蛇
        this.snake = new Snake(this.el)
        this.snake.create()

        this.t = null
        
    }
    start(){
        // 点击开始按钮后，计时器开始
        this.t = setInterval(()=>{
            //蛇移动
            this.snake.move()
            //判断食物
            //获取蛇头X 蛇头Y
            let snakeHeadX = this.snake.snakeObj[0].x*20
            let snakeHeadY = this.snake.snakeObj[0].y*20
            //获取食物的X和Y
            let foodX = this.food.left
            let foodY = this.food.top
            //遍历蛇
            for(let i = this.snake.snakeObj.length-1;i>0;i--){
                //获取蛇尾XY
                let snakeX = this.snake.snakeObj[i].x*20
                let snakeY = this.snake.snakeObj[i].y*20
                //判断自己吃自己
                if((snakeHeadX == snakeX) && (snakeHeadY ==snakeY)){
                    this.stop()
                    return
                }
            }
            //吃东西加长度
            if(snakeHeadX ==foodX && snakeHeadY == foodY){
                //则蛇尾push一格
                this.snake.snakeObj.push({ 
                    x: this.snake.snakeObj[this.snake.snakeObj.length-1].x,
                    y :this.snake.snakeObj[this.snake.snakeObj.length-1].y,
                    color:'blue', 
                    ele:null
                })
                //食物出现，蛇动
                this.snake.move()
                this.food.move()
            }
        },150)
        // 判断蛇头方向
        document.onkeydown = (evt)=>{
            let e = evt || window.event
            //获取键盘
            let keyCode = e.keyCode || e.which
            //判断
            switch(keyCode){
                case 38:
                    this.snake.direction = 'top'
                    break;
                case 40:
                    this.snake.direction = 'bottom'
                    break;
                case 37:
                    this.snake.direction = 'left'
                    break;
                case 39:
                    this.snake.direction = 'right'
                    break;
                default:
                    break;  
            }

        }
    }
    // 停止按钮
    stop(){
        // 清除定时器
        clearInterval(this.t)
        // 弹出得分 等于蛇节长度-3
        let score = this.snake.snakeObj.length-3
        if(score<=10){
            alert(`游戏结束！找个班上吧:${score}分`)
        }else if(score>10 && score<=20){
            alert(`游戏结束！小趴菜，找个班上吧!你的分数是:${score}分`)
        }else if(score>20 && score<=50){
            alert(`游戏结束！你不够菜啊!${score}分`)
        }else if(score>50){
            alert(`游戏结束！小垃圾可真厉害!${score}分`)
        }
        //重置
        this.snake.remove()
        this.snake.direction='right'
        this.snake.snakeObj = [
            { x: 3, y :2, color:'red', ele:null},  //蛇头
            { x: 2, y :2, color:'blue', ele:null}, // 蛇的身体
            { x: 1, y :2, color:'blue', ele:null}  // 蛇的身体
        ]
        this.snake.create()
    }





}